package com.eningqu.aipen.ui.vedio;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.eningqu.aipen.demo.databinding.FragmentVideoBinding;
import com.eningqu.aipen.ui.adapter.VideoAdapter;
import com.eningqu.aipen.ui.base.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class VideoFragment extends BaseFragment<FragmentVideoBinding> {


    List<Bitmap> list=new ArrayList<>();
    RecyclerView recyclerView;
    VideoAdapter adapter;
    public static VideoFragment newInstance() {
        return new VideoFragment();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView=mBinding.rvVideo;
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void async() {
        String pathvideo = "https://db.workercn.cn/media/2023/11/24/C59F0545B3FB46E2BF9BC544B8E891B6/preview.mp4";
        new Thread(() -> {
            Bitmap bitmap=(getNetVideoBitmap(pathvideo));
            list.add(bitmap);
            list.add(bitmap);
            list.add(bitmap);
            list.add(bitmap);
            Objects.requireNonNull(getActivity()).runOnUiThread(() -> adapter.notifyDataSetChanged());
        }).start();
        adapter=new VideoAdapter(list);
        recyclerView.setAdapter(adapter);

    }

    @Override
    protected void initStatusBar() {

    }

    public static Bitmap getNetVideoBitmap(String videoUrl) {
        Bitmap bitmap = null;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根据url获取缩略图
            retriever.setDataSource(videoUrl, new HashMap());
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }
}